package logic

import (
	"context"
	"fmt"
	"wechat/models/mysql"

	"wechat/userrpc/internal/svc"
	"wechat/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type MomentListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMomentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MomentListLogic {
	return &MomentListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MomentListLogic) MomentList(in *userrpc.MomentListRequest) (*userrpc.MomentListResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	mysql.DB.Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	var moments []mysql.Moment
	mysql.DB.Debug().Where("user_id", in.UserId).Find(&moments)

	var MomentLists []*userrpc.MomentList

	for _, moment := range moments {
		list := userrpc.MomentList{
			UserId:   moment.UserId,
			Context:  moment.Context,
			Address:  moment.Address,
			Resource: moment.Resource,
			Like:     moment.Like,
			Id:       int64(moment.ID),
		}

		MomentLists = append(MomentLists, &list)
	}

	return &userrpc.MomentListResponse{List: MomentLists}, nil
}
